1 using UnityEngine;
2 using System.Collections;
3
4 /**
5 * class for handling retrieval, setting and saving of game options
6 */
7 public class OptionsScript : MonoBehaviour {
8
9 private GameControllerScript gameScript;
10
11 public bool use_0; //whether or not to use 0 blocks in the game
12 public bool play_sounds; //whether or not to play sounds
13 public string board_type; //which type of board using: Solid Cube, Hollow Cube, Four Walls, Box Outline
14 public int timer_duration;
15
16 //previous values for detecting change
17 private bool previous_use_0;
18 private bool previous_play_sounds;
19 private string previous_board_type;
20 private int previous_timer_duration;
21
22
23 private GUISkin currentGUISkin;
24 private Vector2 scrollPosition;
25
26 // Use this for initialization
27 void Start () {
28 this.gameScript = this.gameObject.GetComponent ("GameControllerScript") as GameControllerScript;
29 this.currentGUISkin = gameScript.currentGUISkin;
30 this.InitOptions();
31 }
32
33 // Update is called once per frame
34 void Update () {
35
36 }
37
38 public string GetOptionsKey() {
39
40 string optionsKey = "Board Type: " + this.board_type;
41
42 optionsKey += " / Use Zeros: ";
43 if (this.use_0) {
44 optionsKey += "Yes";
45 }
46 else {
47 optionsKey += "No";
48 }
49
50 optionsKey += " / Timer: ";
51 if (this.timer_duration == 0) {
52 optionsKey += "none";
53 }
54 else {
55 optionsKey += this.timer_duration.ToString () + " seconds";
56 }
57
58 return optionsKey;
59 }
60
61 /**
62 * Set up the options in PlayerPrefs if they do not already exist
63 */
64
65 public void InitOptions() {
66 this.initOption ("use_0", false);
67 this.initOption ("play_sounds", true);
68 this.initOption ("board_type", "No Corners");
69 this.initOption ("timer_duration", 0);
70 }
71 //Boolean init option
72 private void initOption(string optionKey, bool defaultValue) {
73 if (!PlayerPrefs.HasKey ("options_" + optionKey)) {
74 this.setOption (optionKey, defaultValue);
75 }
76 else {
77 if (PlayerPrefs.GetInt ("options_" + optionKey) == 1) {
78 this.setOption(optionKey, true);
79 }
80 else {
81 this.setOption(optionKey, false);
82 }
83 }
84 }
85
86 //string init option
87 private void initOption(string optionKey, string defaultValue) {
88 if (!PlayerPrefs.HasKey ("options_" + optionKey)) {
89 this.setOption (optionKey, defaultValue);
90 }
91 else {
92 this.setOption (optionKey, PlayerPrefs.GetString ("options_" + optionKey));
93 }
94 }
95
96
97 //int init option
98 private void initOption(string optionKey, int defaultValue) {
99 if (!PlayerPrefs.HasKey ("options_" + optionKey)) {
100 this.setOption (optionKey, defaultValue);
101 }
102 else {
103 this.setOption (optionKey, PlayerPrefs.GetInt("options_" + optionKey));
104 }
105 }
106
107 //boolean set option
108 private void setOption(string optionKey, bool optionBoolValue) {
109 //convert bool to int
110 int optionIntValue;
111 if (optionBoolValue == true) {
112 optionIntValue = 1;
113 }
114 else {
115 optionIntValue = 0;
116 }
117
118 //set the Player Prefis Value
119 PlayerPrefs.SetInt ("options_" + optionKey,optionIntValue);
120 PlayerPrefs.Save();
121
122 //I want to do this but getting a null reference exception for "this" argh!!
123 //GetType().GetProperty(optionKey).SetValue(this, optionBoolValue, null);
124 //GetType().GetProperty("previous_" + optionKey).SetValue(this, optionBoolValue, null);
125
126 if(optionKey == "use_0") {
127 this.use_0 = optionBoolValue;
128 this.previous_use_0 = optionBoolValue;
129 }
130 if(optionKey == "play_sounds") {
131 this.play_sounds = optionBoolValue;
132 this.previous_play_sounds = optionBoolValue;
133 }
134 }
135
136 //string set option
137 private void setOption(string optionKey, string optionStringValue) {
138 //set the Player Prefis Value
139 PlayerPrefs.SetString ("options_" + optionKey, optionStringValue);
140 PlayerPrefs.Save();
141
142 //I want to do this but getting a null reference exception for "this" argh!!
143 //GetType().GetProperty(optionKey).SetValue(this, optionBoolValue, null);
144 //GetType().GetProperty("previous_" + optionKey).SetValue(this, optionBoolValue, null);
145
146 if(optionKey == "board_type") {
147 this.board_type = optionStringValue;
148 this.previous_board_type = optionStringValue;
149 }
150 }
151
152 //int set option
153 private void setOption(string optionKey, int optionValue) {
154 //set the Player Prefis Value
155 PlayerPrefs.SetInt ("options_" + optionKey, optionValue);
156 PlayerPrefs.Save();
157
158 //I want to do this but getting a null reference exception for "this" argh!!
159 //GetType().GetProperty(optionKey).SetValue(this, optionBoolValue, null);
160 //GetType().GetProperty("previous_" + optionKey).SetValue(this, optionBoolValue, null);
161
162 if(optionKey == "timer_duration") {
163 this.timer_duration = optionValue;
164 this.previous_timer_duration = optionValue;
165 }
166 }
167
168 void OnGUI() {
169 if (this.gameScript.gameView == "options") {
170 this.gameScript.mainCamera.transform.eulerAngles = new Vector3 (120, 23, 0);
171
172 GUI.skin = this.gameScript.currentGUISkin;
173
174 //check to see if any options changed
175 if (previous_use_0 != use_0) {
176 this.setOption ("use_0", this.use_0);
177 GameControllerScript.performRestart = true;
178 }
179
180 if (this.previous_board_type != this.board_type) {
181 this.setOption ("board_type", this.board_type);
182 GameControllerScript.performRestart = true;
183 }
184
185 if (previous_play_sounds != play_sounds) {
186 this.setOption ("play_sounds", this.play_sounds);
187 }
188
189 if (this.previous_timer_duration != this.timer_duration) {
190 this.setOption ("timer_duration", this.timer_duration);
191 GameControllerScript.performRestart = true;
192 }
193
194
195
196 //set the label
197 GUILayout.Label ("Options", "BigLabel");
198
199 scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(Screen.width), GUILayout.Height(Mathf.Ceil(Screen.height * .80f)));
200
201
202
203 //Sounds
204 GUILayout.BeginHorizontal ();
205 if (GUILayout.Toggle (this.play_sounds, "Play Sounds", currentGUISkin.toggle)) {
206 this.play_sounds = true;
207 }
208 else {
209 this.play_sounds = false;
210 }
211 GUILayout.Label ( "Play Sounds", "ToggleLabel");
212 GUILayout.EndHorizontal();
213
214
215 GUILayout.Label ("WARNING! Changing any of the following options will cause the game to reset!", "ToggleLabelWarning");
216
217 GUILayout.Label ("Game Board Type", "Subheader");
218 //Game Board Type
219 GUILayout.BeginHorizontal ();
220 if (GUILayout.Toggle (this.board_type == "Solid Cube", "Solid Cube", currentGUISkin.toggle)) {
221 this.board_type = "Solid Cube";
222 }
223 GUILayout.Label ( "Solid Cube (27 Blocks)", "ToggleLabel");
224 GUILayout.EndHorizontal();
225
226
227 GUILayout.BeginHorizontal ();
228 if (GUILayout.Toggle (this.board_type == "Hollow Cube", "Hollow Cube", currentGUISkin.toggle)) {
229 this.board_type = "Hollow Cube";
230 }
231 GUILayout.Label ( "Hollow Cube (26 Blocks)", "ToggleLabel");
232 GUILayout.EndHorizontal();
233
234
235 GUILayout.BeginHorizontal ();
236 if (GUILayout.Toggle (this.board_type == "Four Walls", "Four Walls", currentGUISkin.toggle)) {
237 this.board_type = "Four Walls";
238 }
239 GUILayout.Label ( "Four Walls (24 Blocks)", "ToggleLabel");
240 GUILayout.EndHorizontal();
241
242
243 GUILayout.BeginHorizontal ();
244 if (GUILayout.Toggle (this.board_type == "Box Outline", "Box Outline", currentGUISkin.toggle)) {
245 this.board_type = "Box Outline";
246 }
247 GUILayout.Label ( "Cube Outline (20 Blocks)", "ToggleLabel");
248 GUILayout.EndHorizontal();
249
250
251
252 GUILayout.BeginHorizontal ();
253 if (GUILayout.Toggle (this.board_type == "No Corners", "No Corners", currentGUISkin.toggle)) {
254 this.board_type = "No Corners";
255 }
256 GUILayout.Label ( "No Corners (19 Blocks)", "ToggleLabel");
257 GUILayout.EndHorizontal();
258
259
260 GUILayout.BeginHorizontal ();
261 if (GUILayout.Toggle (this.board_type == "No Corners/Center", "No Corners/Center", currentGUISkin.toggle)) {
262 this.board_type = "No Corners/Center";
263 }
264 GUILayout.Label ( "No Corners/Center (18 Blocks)", "ToggleLabel");
265 GUILayout.EndHorizontal();
266
267
268 //TIMER OPTIONS ####################################################
269 GUILayout.Label ("Timer", "Subheader");
270
271 foreach (int i in this.GetTimerDurationTimes())
272 {
273 GUILayout.BeginHorizontal ();
274 if (GUILayout.Toggle (this.timer_duration == i, "", currentGUISkin.toggle)) {
275 this.timer_duration = i;
276 }
277 GUILayout.Label (TimerDurationToString (i), "ToggleLabel");
278 GUILayout.EndHorizontal();
279 }
280
281
282
283
284 //Other OPTIONS ####################################################
285 GUILayout.Label ("Block Numbers", "Subheader");
286
287 //Use Zeros
288 GUILayout.BeginHorizontal ();
289 if (GUILayout.Toggle (use_0, "Use 0s (Note: Changing this will reset the current game!)", currentGUISkin.toggle)) {
290 use_0 = true;
291 }
292 else {
293 use_0 = false;
294 }
295 GUILayout.Label ( "Use Zeros", "ToggleLabel");
296 GUILayout.EndHorizontal();
297
298
299
300 foreach (Touch touch in Input.touches) {
301 if (touch.phase == TouchPhase.Moved)
302 {
303 // dragging
304 scrollPosition.y += touch.deltaPosition.y;
305 }
306 }
307
308 GUILayout.EndScrollView();
309
310 if (GUILayout.Button ("Return to Menu", "Button")) {
311 gameScript.gameView = "menu";
312 }
313 }
314 }
315
316 private int[] GetTimerDurationTimes() {
317 int[] times = new int[4]{0,25,20,15};
318 return times;
319 }
320
321 private string TimerDurationToString(int timerDuration) {
322 if (timerDuration == 0) {
323 return "No Timer";
324 }
325 if (timerDuration == 25) {
326 return "Light Speed (25 Sec)";
327 }
328 if (timerDuration == 20) {
329 return "Ridiculous Speed (20 Sec)";
330 }
331 if (timerDuration == 15) {
332 return "Ludicrous Speed (15 Sec)";
333 }
334 return "Unknown Speed";
335 }
336 }
public bool use_0; whether or not to use 0 blocks in the game
public bool play_sounds; whether or not to play sounds
public string board_type; which type of board using: Solid Cube, Hollow Cube, Four Walls, Box Outline
previous values for detecting change
Use this for initialization
Update is called once per frame
Boolean init option
string init option
int init option
boolean set option
convert bool to int
set the Player Prefis Value
I want to do this but getting a null reference exception for "this" argh!!
GetType().GetProperty(optionKey).SetValue(this, optionBoolValue, null);
GetType().GetProperty("previous_" + optionKey).SetValue(this, optionBoolValue, null);
string set option
set the Player Prefis Value
I want to do this but getting a null reference exception for "this" argh!!
GetType().GetProperty(optionKey).SetValue(this, optionBoolValue, null);
GetType().GetProperty("previous_" + optionKey).SetValue(this, optionBoolValue, null);
int set option
set the Player Prefis Value
I want to do this but getting a null reference exception for "this" argh!!
GetType().GetProperty(optionKey).SetValue(this, optionBoolValue, null);
GetType().GetProperty("previous_" + optionKey).SetValue(this, optionBoolValue, null);
check to see if any options changed
set the label
Sounds
Game Board Type
TIMER OPTIONS ####################################################
Other OPTIONS ####################################################
Use Zeros
dragging